/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmVector.java
 *
 * Created on 02-12-2014, 10:05:33 AM
 */
package presentacion;

import Negocios.VectorEntero;
import Negocios.Entero;

import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Estudiante
 */
public class FrmVector extends javax.swing.JFrame {

    /** Creates new form FrmVector */
    public FrmVector() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btnDimencion = new javax.swing.JButton();
        txtDim = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnOrdenar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Dimencion");

        btnDimencion.setText("Dimencionar");
        btnDimencion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDimencionActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        btnOrdenar.setText("Ordenar");
        btnOrdenar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOrdenarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtDim, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnDimencion)
                        .addGap(337, 337, 337))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnOrdenar)
                        .addContainerGap(502, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtDim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDimencion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnOrdenar)
                .addContainerGap(204, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void Dimencionar(int CantidadColumna){
        DefaultTableModel modelo = new DefaultTableModel();
        this.jTable1.setModel(modelo);
        for(int i=0;i<=CantidadColumna-1;i++){
            modelo.addColumn(Integer.toString(i));            
        }
        Object[] fila = new Object[1];
        modelo.addRow(fila);
    }
    
    private void btnDimencionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDimencionActionPerformed
        // TODO add your handling code here:
        this.Dimencionar(Integer.parseInt(this.txtDim.getText()));
    }//GEN-LAST:event_btnDimencionActionPerformed

    private void CargarVector(VectorEntero ObjVector){
        for(int i=0;i<=Integer.parseInt(this.txtDim.getText())-1;i++){
            Entero ObjEntero = new Entero();
            ObjEntero.setNum(Integer.parseInt(jTable1.getValueAt(0, i).toString()));
            ObjVector.Insertar(ObjEntero);
        }
    }
    
    private void MostrarVector(VectorEntero ObjVector){
        this.Dimencionar(ObjVector.getN());
        for(int i=0;i<=ObjVector.getN()-1;i++){
            jTable1.setValueAt(ObjVector.LeerVector(i).getNum(),0, i);
        }
    }
    private void btnOrdenarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOrdenarActionPerformed
        // TODO add your handling code here:
        VectorEntero ObjVector = new VectorEntero();
        this.CargarVector(ObjVector);
        ObjVector.Ordenar();
        this.MostrarVector(ObjVector);
    }//GEN-LAST:event_btnOrdenarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FrmVector().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDimencion;
    private javax.swing.JButton btnOrdenar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField txtDim;
    // End of variables declaration//GEN-END:variables
}
